﻿using HalconDotNet;
using Halcon二次开发.Model.自定义控件;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.工位监控画面
{
    public partial class FramesTimeForm : Form
    {
        // Win32 API 导入
        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        [DllImport("user32.dll")]
        private static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);

        // 常量定义
        private const uint SC_CLOSE = 0xF060;  // 关闭按钮的命令标识符
        private const uint MF_GRAYED = 0x00000001; // 灰显不可用
        private const uint MF_BYCOMMAND = 0x00000000; // 按命令标识符操作
        //自定义控件窗体,展示数据
        public HwindowFlowNodeUrseControl HwindowFlowNodeUrseControl;
        //当前工位名称
        public string name;
        public FramesTimeForm(HwindowFlowNodeUrseControl HwindowFlowNodeUrseControl, MonitoringForm monitoringForm)
        {
            InitializeComponent();
            uiSplitContainer1.SplitterDistance = uiSplitContainer1.Width / 2;
            this.HwindowFlowNodeUrseControl = HwindowFlowNodeUrseControl;
            this.Text = "工程号:"+HwindowFlowNodeUrseControl.projectName + HwindowFlowNodeUrseControl.name + "工位画面监视";
            name= HwindowFlowNodeUrseControl.name;
            customBarChartControl1.Data = HwindowFlowNodeUrseControl.ngDictionary;
            //将自定义控件添加到窗体当中
            this.MdiParent = monitoringForm;
            this.Show();
            this.SizeChanged += (s, e) => DisableCloseButton();
        }

        private void DisableCloseButton()
        {
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            if (hMenu != IntPtr.Zero)
            {
                // 禁用关闭按钮
                EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
            }
        }

        private void FramesTimeForm_Load(object sender, EventArgs e)
        {
            // 强制创建句柄并禁用关闭按钮
            IntPtr handle = this.Handle; // 强制句柄生成
            IntPtr hMenu = GetSystemMenu(handle, false);
            if (hMenu != IntPtr.Zero)
            {
                EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
            }
        }
        /// <summary>
        /// 复位按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            HwindowFlowNodeUrseControl.toolStripButton4_Click(sender,null);
        }
    }
}
/*
 * 
 *  //获取到运行界面上的画面
                        HOperatorSet.DumpWindowImage(out HObject image, HwindowFlowNodeUrseControl.hSmartWindowControl1.HalconWindow);
                        //对该界面进行赋值
                        HOperatorSet.DispObj(image, this.Hwindow.HalconWindow);
 */
